﻿using System;
using System.Collections.Generic;
using System.Linq;
using CFrame.Common;
using System.Text;
using UnityEngine;
using CFrame.UI;
using CFrame.Tools;

/*
 * 脚本功能：UI管理器
 * 编写日期：2018.12.13
 * 编写人：HYZ
 */
namespace CFrame.Manager
{
    public class UIManager:BaseManager
    {
        //public UIManager(GameMiddle game) : base(game) { }

        //全局的UI父级物体
        private Transform uiRoot;
        /// <summary>
        /// UI根节点
        /// </summary>
        public Transform UIRoot
        {
            get
            {
                if(uiRoot == null)
                {
                    GameObject go = GameObject.FindObjectOfType<Canvas>().gameObject;
                    if(go == null)
                    {
                        go = new GameObject("Canvas");
                        uiRoot = go.transform;
                    }
                }
                return uiRoot;
            }

            set
            {
                uiRoot = value;
            }
        }

        /// <summary>
        /// 存储所有界面的路径
        /// </summary>
        private Dictionary<string, string> uiPathDict;

        /// <summary>
        /// 界面栈,用来做界面切换
        /// </summary>
        private Stack<BaseUI> uiStack;

        /// <summary>
        /// 存储所有的UI对象
        /// </summary>
        private Dictionary<string, BaseUI> uiDict;

        /// <summary>
        /// 要去切换的界面
        /// </summary>
        private string uiTypeToPush = string.Empty;


        public override void OnInit()
        {
            
        }

        public override void OnUpdate()
        {
            if (!string.IsNullOrEmpty(uiTypeToPush))
            {
                PushUI(uiTypeToPush);
                uiTypeToPush = string.Empty;
            }
        }

        public override void OnDestroy()
        {

        }

        /// <summary>
        /// 子线程调用切换UI
        /// </summary>
        /// <param name="uiType"></param>
        public void PushUISync(string uiType)
        {
            uiTypeToPush = uiType;
        }

        /// <summary>
        /// 主线程切换UI
        /// </summary>
        /// <param name="uiType"></param>
        /// <returns></returns>
        public BaseUI PushUI(string uiType)
        {
            if(uiStack == null)
            {
                uiStack = new Stack<BaseUI>();
            }

            //获取UI并切换
            BaseUI ui = GetUI(uiType);
            if (ui == null)
            {
                Debuger.LogError("不存在UI类型-->"+uiType);
                return null;
            }

            //判断栈中是否存在界面
            if (uiStack.Count > 0)
            {
                //调用界面暂停的方法
                BaseUI topUI = uiStack.Peek();
                topUI.OnPause();
            }

            ui.OnEnter();
            //压入栈中
            uiStack.Push(ui);
            return ui;
        }

        /// <summary>
        /// 返回上一个界面
        /// </summary>
        public void PopUI()
        {
            if(uiStack == null)
            {
                uiStack = new Stack<BaseUI>();
            }

            if(uiStack.Count <= 0)
            {
                return;
            }
            //移除顶部UI
            BaseUI topUI = uiStack.Pop();
            topUI.OnExit();

            if(uiStack.Count <= 0)
            {
                return;
            }
            //显示下一级UI
            BaseUI bottomUI = uiStack.Peek();
            bottomUI.OnResume();
        }

        /// <summary>
        /// 获取UI对象
        /// </summary>
        /// <param name="uiType"></param>
        /// <returns></returns>
        private BaseUI GetUI(string uiType)
        {
            try
            {
                if (uiDict == null)
                {
                    uiDict = new Dictionary<string, BaseUI>();
                }
                BaseUI ui = uiDict.TryGet(uiType);
                if (ui == null)
                {
                    //找到面板路径然后实例化
                    string path = uiPathDict.TryGet(uiType);
                    if (path == null)
                    {
                        return null;
                    }
                    GameObject go = GameObject.Instantiate(Resources.Load(path)) as GameObject;
                    go.transform.SetParent(UIRoot, false);
                    ui = go.GetComponent<BaseUI>();
                    ui.UIMng = this;
                    ui.Game = game;
                    uiDict.Add(uiType, ui);
                    return ui;
                }
                else
                {
                    return ui;
                }
            }catch(Exception e)
            {
                Debuger.LogError("GetUI Error --> "+e);
                return null;
            }
        }

        /// <summary>
        /// 注册UI面板对应的路径信息
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public void AddUIPath(string key,string value)
        {
            if(uiPathDict == null)
            {
                uiPathDict = new Dictionary<string, string>();
            }

            if (!uiPathDict.ContainsKey(key))
            {
                uiPathDict.Add(key, value);
            }
        }
    }
}
